package shell.model
{
	import flash.filesystem.File;	
	import flash.filesystem.FileMode;	
	import flash.filesystem.FileStream;		import flash.data.EncryptedLocalStore;	
	import flash.utils.ByteArray;			import shell.constants.ExpenseManagerConstants;	
	import shell.vo.LoginItemVO;	
	
	import mx.collections.ArrayCollection;	
		import org.puremvc.as3.multicore.utilities.fabrication.patterns.proxy.FabricationProxy;

	public class ExpenseManagerLoginProxy extends FabricationProxy
	{
		public static const NAME:String = "ExpenseManagerLoginProxy";
		
		private var _loginUsersColl:ArrayCollection;
		private var _currentUserInfo:LoginItemVO;
		private var loginDetailsXML:XML;
		//private var loginXMLFile:File;
		
		public function ExpenseManagerLoginProxy(name:String=null, data:Object=null)
		{
			super(NAME, data);
		}
		
		override public function onRegister():void
		{
			loadLoginDetails();
		}
		
		/*
		 * method to load loginDetails xml
		 */
		private function loadLoginDetails():void
		{
			var byteArray : ByteArray = new ByteArray(); 
			byteArray = EncryptedLocalStore.getItem("loginDetails");
			if(byteArray != null)
			{
				loginDetailsXML = XML(byteArray.readUTFBytes(byteArray.length));
			}
			else
			{
				loginDetailsXML = <login><users></users></login>;
			}
			
			populateLoginDetails(loginDetailsXML);
		}

		/*
		 * method to populate the loginDetails
		 */
		private function populateLoginDetails(loginDetails:XML):void
		{
			_loginUsersColl = new ArrayCollection();
			var loginUsersList:XMLList = loginDetails.users.user;
			for(var i:int=0; i< loginUsersList.length(); i++)
			{
				var loginUser:XML = loginUsersList[i];
				var loginItemVO:LoginItemVO = new LoginItemVO();
				loginItemVO.userName = loginUser.@name;
				loginItemVO.userPassword = loginUser.@password;
				loginItemVO.savePassword = (loginUser.@savePassword == "true")? true : false;
				_loginUsersColl.addItem(loginItemVO);
			}
			
			sendNotification(ExpenseManagerConstants.SHOW_LOGIN_WINDOW, _loginUsersColl);
		}
		
		/*
		 * method to add new user entry to the xml and save it
		 */
		public function addNewuserEntry(loginItemVO:LoginItemVO):void
		{
			var userEntry:XML = <user></user>;
			userEntry.@name = loginItemVO.userName;
			userEntry.@password = loginItemVO.userPassword;
			userEntry.@savePassword = loginItemVO.savePassword;
			
			loginDetailsXML.users.appendChild(userEntry);
			
			var newXMLStr:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + loginDetailsXML.toXMLString();
			var filedataArray : ByteArray = new ByteArray();
			
			filedataArray.writeUTFBytes(newXMLStr);
			EncryptedLocalStore.setItem("loginDetails",filedataArray);
			
			var userDataFile:File = new File();
			userDataFile.nativePath = File.applicationDirectory.nativePath+"/xml/"+loginItemVO.userName+"_expenseDetails.xml";
			var newDataXMLStr:String = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><expense></expense>";
			userDataFile.resolvePath(userDataFile.nativePath);
			var fs:FileStream = new FileStream();
			fs.open(userDataFile, FileMode.WRITE);
			fs.writeUTFBytes(newDataXMLStr);
			fs.close();
		}

		/*
		 * loginUsersColl: array collection that holds user credentials
		 */
		public function set loginUsersColl(value:ArrayCollection):void
		{
			_loginUsersColl = value;
		}
		
		public function get loginUsersColl():ArrayCollection
		{
			return _loginUsersColl as ArrayCollection;
		}
		
		/*
		 * currentUserInfo: to store the current user information
		 */
		public function set currentUserInfo(value:LoginItemVO):void
		{
			_currentUserInfo = value;
		}
		
		public function get currentUserInfo():LoginItemVO
		{
			return _currentUserInfo as LoginItemVO;
		}
	}
}